<?php
class SF_Ulmart_ProductController extends Mage_Core_Controller_Front_Action
{
	public function indexAction()
	{
		$this->loadLayout();
		$this->renderLayout();
	}

	public function saveAction()
	{
		$data = $this->getRequest()->getParams();
		if($data){
			try {
				$model = Mage::getModel('ulmart/product');
				$productId = $data['data']['product_id'];
				$model->load($productId, 'product_id');
				$model->setData('product_id',$data['data']['product_id']);
				$model->setData('is_selected',$data['data']['is_selected']);
				$model->setData('comment',$data['data']['comment']);
				$model->setData('is_confirm',$data['data']['is_confirm']);
				$model->save();
				
				echo $this->__('saved');
			} catch (Exception $e) {
				echo $this->__($e->getMessage());
			}
		}
	}
	
 	public function downloadimageAction(){
		$pId = $this->getRequest()->getParam('id');
		$product = Mage::getModel('catalog/product')->load($pId);
		if($product->getId()){
			$imgs = array();
			$images = $product->getMediaGalleryImages();
			foreach ($images as $image){
				$imgs[] = $image->getUrl();
			}
			
			$filename = 'tmp.zip';
			$zip = new ZipArchive();
			$zip->open($filename, ZipArchive::OVERWRITE);
			
			$flag = false; /*检测起码至少有一张图*/
			foreach ($imgs as $key=>$vo) {
				$fileData = file_get_contents($vo);
				if ($fileData) {
					$zip->addFromString($key.'.jpg', $fileData);
					$flag = true;
				}
			}
			if(!$flag) return false;
			$downloadFileName = str_replace(' ','-',$product->getSku());
			$zip->close();
			$file = fopen($filename, "r");
			Header("Content-type: application/octet-stream");
			Header("Accept-Ranges: bytes");
			Header("Accept-Length: " . filesize($filename));
			Header("Content-Disposition: attachment; filename=". $downloadFileName .".zip");
			$buffer = 1024; //
			while (!feof($file)) {
				$file_data = fread($file, $buffer);
				echo $file_data;
			}
			fclose($file);
			unlink($filename); //删除文件

		}

	}
}